package top.gangster.lunan.userservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import top.gangster.lunan.userservice.entity.Role;
import top.gangster.lunan.userservice.entity.vo.RoleVo;
import top.gangster.lunan.userservice.mapper.RoleMapper;
import top.gangster.lunan.userservice.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 角色表，角色id不仅映射了该角色的名称，而且代表了该角色所对应的权限等级 服务实现类
 * </p>
 *
 * @author vic
 * @since 2021-11-06
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

  @Autowired
  RoleService roleService;
  @Resource
  RoleMapper roleMapper;

  /**
   * 查找角色
   * @param current 当前页
   * @param size 当前页大小
   * @return RoleVo
   */
  @Override
  public RoleVo getRoleList(int current,int size){
    RoleVo roleVo = new RoleVo();
    Page<Role> page = new Page<>(current,size);
    IPage<Role> res = roleMapper.selectPageRoleList(page);
    roleVo.setTotal(page.getTotal());
    roleVo.setRoleList(page.getRecords());
    roleVo.setCurrent(current);
    roleVo.setSize(size);
    return roleVo;
  }

  /**
   * 不分页查找角色
   * @return List<Role>
   */
  @Override
  public List<Role> getRoleList(){
    QueryWrapper<Role> qw = new QueryWrapper<>();
    return roleService.list(qw);
  }

  /**
   * 通过查找名字查找角色
   * @param current 当前页
   * @param size 当前页大小
   * @param roleName 角色名
   * @return RoleVo
   */
  @Override
  public RoleVo getRoleByName(int current,int size,String roleName){
    RoleVo roleVo = new RoleVo();
    Page<Role> page = new Page<>(current,size);
    IPage<Role> res = roleMapper.selectPageRoleByName(page,roleName);
    roleVo.setTotal(page.getTotal());
    roleVo.setRoleList(page.getRecords());
    roleVo.setCurrent(current);
    roleVo.setSize(size);
    return roleVo;
  }

  /**
   * 添加角色
   * @param role 角色
   * @return RoleVo
   */
  @Override
  public boolean addRole(Role role){
    System.out.println("add");
    System.out.println(role.getRoleName());
    System.out.println(role.getAuthId());
    Role roles = new Role();
    roles.setAuthId(role.getAuthId());
    roles.setRoleName(role.getRoleName());
    return roleService.save(role);
  }

  /**
   * 修改角色
   * @param role 角色
   * @return RoleVo
   */
  @Override
  public boolean modifyRole(Role role){
    System.out.println("modi");
    System.out.println(role.getRoleId());
    System.out.println(role.getRoleName());
    System.out.println(role.getAuthId());
    Role roles = new Role();
    roles.setRoleId(role.getRoleId());
    roles.setRoleName(role.getRoleName());
    roles.setAuthId(role.getAuthId());
    return roleService.updateById(role);
  }

  /**
   * 删除角色
   * @param role 角色
   * @return RoleVo
   */
  @Override
  public boolean deleteRole(Role role){
    return roleService.removeById(role);
  }

}
